﻿using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using System;
using System.Collections.Generic;
using WinRT.SIP.UILogic.Models;
using WinRT.SIP.UILogic.Repositories;

namespace WinRT.SIP.UILogic.ViewModels
{
    public class NetworkSettingsFlyoutViewModel : BindableBase, IFlyoutViewModel
    {
        private static readonly List<string> transportProtocols = new List<string> { "UDP", "TCP" };

        private readonly INetworkSettingsRepository _networkSettingsRepository;

        private Action _closeFlyout;
        private Action _goBack;

        private string _realm;
        private string _proxyHost;
        private string _proxyPort;
        private string _transport = transportProtocols[0];

        public DelegateCommand GoBackCommand { get; private set; }
        public DelegateCommand SaveCommand { get; private set; }

        public Action CloseFlyout
        {
            get { return _closeFlyout; }
            set { SetProperty(ref _closeFlyout, value); }
        }

        public Action GoBack
        {
            get { return _goBack; }
            set { SetProperty(ref _goBack, value); }
        }

        public string Realm
        {
            get { return _realm; }
            set { SetProperty(ref _realm, value); }
        }

        public string ProxyHost
        {
            get { return _proxyHost; }
            set { SetProperty(ref _proxyHost, value); }
        }

        public string ProxyPort
        {
            get { return _proxyPort; }
            set { SetProperty(ref _proxyPort, value); }
        }

        public List<string> TransportProtocols
        {
            get { return transportProtocols; }
        }

        public string Transport
        {
            get { return _transport; }
            set { SetProperty(ref _transport, value); }
        }

        public async void Open(object parameter, Action successAction)
        {
            var networkSettings = await _networkSettingsRepository.LoadAsync();
            if (networkSettings != null)
            {
                Realm = networkSettings.Realm;
                ProxyHost = networkSettings.ProxyHost;
                ProxyPort = networkSettings.ProxyPort;
                Transport = networkSettings.Transport;
            }
        }

        public NetworkSettingsFlyoutViewModel(INetworkSettingsRepository networkSettingsRepository)
        {
            _networkSettingsRepository = networkSettingsRepository;

            GoBackCommand = new DelegateCommand(() => GoBack());
            SaveCommand = new DelegateCommand(async () =>
                {
                    CloseFlyout();
                    await _networkSettingsRepository.SaveAsync(new NetworkSettings
                    {
                        Realm = _realm,
                        ProxyHost = _proxyHost,
                        ProxyPort = _proxyPort,
                        Transport = _transport
                    });
                });
        }
    }
}
